package com.enation.app.javashop.api.client;


import com.enation.app.javashop.model.trade.cart.dos.OrderPermission;
import com.enation.app.javashop.model.trade.order.dos.OrderDO;
import com.enation.app.javashop.model.trade.order.dos.OrderLogDO;
import com.enation.app.javashop.model.trade.order.dos.OrderOutStatus;
import com.enation.app.javashop.model.trade.order.dos.TransactionRecord;
import com.enation.app.javashop.model.trade.order.enums.OrderOutStatusEnum;
import com.enation.app.javashop.model.trade.order.enums.OrderOutTypeEnum;
import com.enation.app.javashop.model.trade.order.enums.OrderServiceStatusEnum;
import com.enation.app.javashop.model.trade.order.vo.CancelVO;
import com.enation.app.javashop.service.trade.order.*;
import com.enation.app.javashop.service.trade.snapshot.GoodsSnapshotManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 订单操作Controlle
 *
 * @author fk create in 2020/4/7
 * @version v2.0
 * @since v7.2.0
 */
@RestController
@RequestMapping("/client/orders")
@Validated
@ApiIgnore
public class OrderOperateClientController {

    @Autowired
    private OrderOperateManager orderOperateManager;

    @Autowired
    private GoodsSnapshotManager goodsSnapshotManager;

    @Autowired
    private OrderOutStatusManager orderOutStatusManager;

    @Autowired
    private TransactionRecordManager transactionRecordManager;

    @Autowired
    private OrderPayManager orderPayManager;

    @Autowired
    private OrderLogManager orderLogManager;

    @PutMapping("/{order_sn}/item-json")
    public void updateItemJson(@RequestParam("items_json") String itemsJson, @PathVariable("order_sn") String orderSn) {

        orderOperateManager.updateItemJson(itemsJson, orderSn);
    }

    @PostMapping("/goods-snapshot")
    public void addGoodsSnapshot(@RequestBody OrderDO orderDO) {
        goodsSnapshotManager.add(orderDO);
    }

    @PutMapping("/client/orders/shop/{shop_id}/shop-name")
    public void editOrderShopName(@PathVariable("shop_id") Long shopId, @RequestParam("shop_name") String shopName) {
        orderOperateManager.editOrderShopName(shopId, shopName);
    }

    @PutMapping("/cancel")
    public void cancel(@RequestBody CancelVO cancelVO, @RequestParam("permission") OrderPermission permission) {
        orderOperateManager.cancel(cancelVO, permission);
    }

    @PutMapping("/{order_sn}/service/status")
    public void updateServiceStatus(@PathVariable("order_sn") String orderSn, @RequestParam("service_status") OrderServiceStatusEnum serviceStatus) {
        orderOperateManager.updateServiceStatus(orderSn, serviceStatus);
    }

    @PutMapping("/{order_sn}/out/status")
    public void editOutStatus(@PathVariable("order_sn") String orderSn, @RequestParam("type_enum") OrderOutTypeEnum typeEnum, @RequestParam("status_enum") OrderOutStatusEnum statusEnum) {
        orderOutStatusManager.edit(orderSn, typeEnum, statusEnum);
    }

    @PostMapping("/transaction-record")
    public TransactionRecord addTransactionRecord(@RequestBody TransactionRecord transactionRecord) {
        return transactionRecordManager.add(transactionRecord);
    }

    @PostMapping("/pay-success")
    public void paySuccess(@RequestParam("trade_type") String tradeType, @RequestParam("sub_sn") String subSn, @RequestParam("return_trade_no") String returnTradeNo, @RequestParam("pay_price") Double payPrice) {

        orderPayManager.paySuccess(tradeType, subSn, returnTradeNo, payPrice);
    }


    @PostMapping("/order-out-status")
    public void add(@RequestBody OrderOutStatus orderOutStatus) {
        orderOutStatusManager.add(orderOutStatus);
    }

    @PostMapping("/order-log")
    public void addOrderLog(@RequestBody OrderLogDO logDO) {
        orderLogManager.add(logDO);
    }

}
